package com.ibuonline.b2b.gather;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.ibuonline.b2b.entity.Article;

public class ArticleGather {
	
	private String titleStartTag = "<h1 id=\"artibodyTitle\"";
	private String contentStartTag = "<div class=\"blkContainerSblkCon\" id=\"artibody\">";
	private String contentEndTag = "class=\"blkComment otherContent_01\"";
	private String imageStartTag = "<div class=\"img_wrapper\">";
	private String sourceStartTag = "<span id=\"media_name\">";
	private String imageDescrTag = "<span class=\"img_descr\">";
	private String downloadDir = "C:\\upload\\temp\\";
	private String charset = "gb2312";
	
	public String readHTML(String link) throws Exception {
		URL url = new URL(link);
		InputStream ins = url.openStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(ins, charset));
		String line;
		StringBuilder sb = new StringBuilder();
		while ((line = reader.readLine()) != null) {
			sb.append(line.replaceAll("(\\s)+", " ") + "\n");
		}
		return sb.toString();
	}
	
	public Article parseHTML(String html) {
		Article article = new Article();
		String title = getInnerText(html, titleStartTag);
		String source = getSource(html, sourceStartTag);
		String sourceLink = getSourceLink(html, sourceStartTag);
		List<String> images = getImages(html, imageStartTag, imageDescrTag);
		String content = getContent(html, contentStartTag, contentEndTag);
		String keywords = getKeywords(html);
		String description = getDescription(html);
		article.setTitle(title);
		article.setSource(source);
		article.setSourceLink(sourceLink);
		article.setContent(content);
		article.setKeywords(keywords);
		article.setDescription(description);
		
		System.out.println("images:" + images);
		return article;
	}
	
	public String getInnerText(String html, String startTag) {
		int pos1 = html.indexOf(startTag);
		if (pos1 == -1)
			return null;
		int pos2 = html.indexOf(">", pos1);
		if (pos2 < pos1)
			return null;
		int pos3 = html.indexOf(getEndTag(startTag), pos2+1);
		if (pos3 < pos2)
			return null;
		return html.substring(pos2 + 1, pos3);
	}
	
	public String getSource(String html, String startTag) {
		String innerHTML = getInnerText(html, startTag);
		if (innerHTML == null)
			return null;
		return innerHTML.replaceAll("<[^>]+>", "");
	}
	
	public String getSourceLink(String html, String startTag) {
		String innerHTML = getInnerText(html, startTag);
		if (innerHTML == null)
			return null;
		int pos1 = innerHTML.indexOf("href=\"");
		if (pos1 == -1)
			return null;
		int pos2 = innerHTML.indexOf('"', pos1+6);
		if (pos2 == -1)
			return null;
		return innerHTML.substring(pos1+6, pos2);
	}
	
	
	public String getKeywords(String html) {
		int pos1 = html.indexOf("<meta name=\"keywords\"");
		if (pos1 == -1)
			pos1 = html.indexOf("<meta name=keywords ");
		if (pos1 == -1)
			return null;
		int pos2 = html.indexOf("content=\"", pos1);
		if (pos2 == -1)
			return null;
		int pos3 = html.indexOf('"', pos2+9);
		if (pos3 == -1)
			return null;
		return html.substring(pos2+9, pos3);
	}
	
	public String getDescription(String html) {
		int pos1 = html.indexOf("<meta name=\"description\"");
		if (pos1 == -1)
			pos1 = html.indexOf("<meta name=description ");
		if (pos1 == -1)
			return null;
		int pos2 = html.indexOf("content=\"", pos1);
		if (pos2 == -1)
			return null;
		int pos3 = html.indexOf('"', pos2+9);
		if (pos3 == -1)
			return null;
		return html.substring(pos2+9, pos3);
	}
	
	public String getContent(String html, String contentTag, String endContentTag) {
		int pos1 = html.indexOf(contentTag);
		if (pos1 == -1)
			return null;
		StringBuilder sb = new StringBuilder();
		int pos2 = html.indexOf("<p>", pos1);
		int endPos = html.indexOf(contentEndTag);
		while (pos2 != -1 && pos2 < endPos) {
			int pos3 = html.indexOf("</p>", pos2+3);
			if (pos3 == -1)
				break;
			sb.append(html.substring(pos2, pos3+4) + "\n");
			pos2 = html.indexOf("<p>", pos3);
		}
		return clearTag(sb.toString());
	}
	
	private String clearTag(String input) {
    //	input = StringUtil.htmlTag2LowerCase(input);
    	input = input.replaceAll("<(b|p|strong|br)>", "&lt;$1&gt;")
					 .replaceAll("</(b|p|strong)>", "&lt;/$1&gt;")
					 .replaceAll("<br/>", "&lt;br/&gt;");
    	
    	input = input.replaceAll("<script[^>]*>[^<]*</script[^>]*>", "")
			 		 .replaceAll("<style[^>]*>[^<]*</style[^>]*>", "")
			 		 .replaceAll("<[^>]+>", "");
    	input = input.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
    	return input;
	}
	
	public List<String> getImages(String html, String imageTag, String imageDescrTag) {
		List<String> images = new ArrayList<String>();
		int pos1 = html.indexOf(imageTag);
		String imageDescrEndTag = getEndTag(imageDescrTag);
		while (pos1 != -1) { 
			int pos2 = html.indexOf("<img", pos1);
			if (pos2 == -1)
				break;
			int pos3 = html.indexOf("src=\"", pos2);
			if (pos3 == -1)
				break;
			int pos4 = html.indexOf("\"", pos3 + 5);
			if (pos4 == -1)
				break;
			String src = html.substring(pos3 + 5, pos4);
			try {
				saveImage(src);
			} catch(Exception e) {
				e.printStackTrace();
			}
			
			int pos5 = html.indexOf(imageDescrTag, pos4);
			if (pos5 == -1)
				break;
			int pos6 = html.indexOf(">", pos5);
			if (pos6 == -1)
				break;
			int pos7 = html.indexOf(imageDescrEndTag, pos6);
			if (pos7 == -1)
				break;
			String descr = html.substring(pos6+1, pos7);
			images.add("\n" + src + "\n" + descr);
			
			pos1 = html.indexOf(imageTag, pos7);
		}
		return images;
	}
	
	private void saveImage(String src) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new URL(src).openStream());
		String suffix = src.substring(src.lastIndexOf('.'));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
				downloadDir + System.currentTimeMillis() + suffix));
		byte[] b = new byte[8096];
		int i;
		while ((i = bis.read(b)) != -1) 
			bos.write(b, 0, i);
		bos.close();
		bis.close();
	}

	public String getEndTag(String startTag) {
		int pos = startTag.indexOf(" ");
		if (pos == -1) {
			return "</" + startTag.substring(1);
		} else {
			return "</" + startTag.substring(1, pos) + ">";
		}
	}
	
	public static void main(String[] args) throws Exception {
		ArticleGather ag = new ArticleGather();
		String html = ag.readHTML("http://news.sina.com.cn/c/2012-06-05/133224539031.shtml");
		System.out.println(ag.parseHTML(html));
		//String html = com.dimeng.common.util.FileUtil.readFromFile("C:/html.txt");
		//ag.parseHTML(html);
		//System.out.println("<p>asdfqwer<strong>werweriod</strong>asfiiwenboi</p>"
		//		.replaceAll("<strong>", "&lt;strong&gt;")
		//		.replaceAll("&lt;", "<").replaceAll("&gt;", ">"));
		//System.out.println("<p>abc</P><b>bold</b><strong>strong</strong><div>abcdefg</div><br/>"
		//		.replaceAll("<(b|p|strong|br)>", "&lt;$1&gt;")
		//		.replaceAll("</(b|p|strong)>", "&lt;/$1&gt;")
		//		.replaceAll("<br/>", "&lt;br/&gt;"));
		System.out.println(java.util.Arrays.asList("aa,bb,cc".split(",")).get(1));
	}
}
